<template>
  <view>
    <u-line></u-line>
    <view style="padding: 30rpx" @click="alterLocation(item.city)" v-for="(item,index) in cityList" :key="index">
      {{item.city}}
    </view>
  </view>
</template>

<script>
import {alterFrontUserLocation, getCityList, getStateList} from "@/api/user";
import alterLocation from "@/pages/my/alterLocation.vue";

export default {
  data() {
    return {
      cityList:[],
      havaState:false,
      location:{}
    };
  },
  onLoad(options) {
    console.log('city')
    console.log(options)
    this.location.country = options.country
    if(options?.state === undefined){
      this.havaState = false
      this.getStateList(options.country)
      uni.setNavigationBarTitle({title: options.country})
    }else {
      this.havaState = true
      this.location.province = options.state
      uni.setNavigationBarTitle({title: options.state })

    }

  },
  methods: {
    async getStateList(country){
      const res = await getStateList(country)
      this.cityList = res.data
    },
    async getCityList(state){
      const res = await getCityList(state)
      this.cityList = res.data
    },
    async alterLocation(city){
      this.location.city = city
      const res = await alterFrontUserLocation(this.location)
      if(res.code == 200){
        uni.$emit("alterUserInfo",{success: true})
        setTimeout(()=>{
          uni.navigateBack({
            delta: this.location?.state === undefined ? 2 : 3
          })
        },500)
      }else{
        uni.showToast({
          title: '设置失败',
          duration: 2000,
          icon: 'none'
        });
      }
    },
  }
}
</script>

<style lang="scss">

</style>
